package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.ViolationsMapper;
import com.example.mybatisplus.model.domain.Admins;
import com.example.mybatisplus.mapper.AdminsMapper;
import com.example.mybatisplus.model.domain.Violations;
import com.example.mybatisplus.service.AdminsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-16
 */
@Service
public class AdminsServiceImpl extends ServiceImpl<AdminsMapper, Admins> implements AdminsService {
    @Autowired
    private AdminsMapper adminsMapper;

    @Override
    public Admins login(Admins admin) {
        QueryWrapper<Admins> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", admin.getLoginName())
                .eq("password", admin.getPassword())
                .eq("is_enabled", true);
        Admins one = baseMapper.selectOne(wrapper);
        if (one != null) {
            SessionUtils.saveCurrentUser(one);
        }
        return one;
    }

    @Override
    public Admins register(Admins admin) {
        QueryWrapper<Admins> wrapper = new QueryWrapper<>();
        wrapper.eq("login_name", admin.getLoginName())
                .eq("token", admin.getToken());
        Admins one = baseMapper.selectOne(wrapper);
        if (one != null) {
            // 如果查询到记录,则更新密码字段
            one.setPassword(admin.getPassword());
            baseMapper.updateById(one);
            return one;
        }else{
            return one;
        }
    }


    //只向前端传递已启用的内容
    @Override
    public List<Admins> getAllAdmins() {
        List<Admins> allAdmins = adminsMapper.getAllAdmins();
        List<Admins> enabledAdmins = new ArrayList<>();

        for (Admins admin : allAdmins) {
            if (Boolean.TRUE.equals(admin.getIsEnabled()) && Boolean.TRUE.equals(!admin.getIsDeleted())) {
                enabledAdmins.add(admin);
            }
        }
        return enabledAdmins;
    }

    @Override
    @Transactional
    public void insertAll(Admins admins) {
        adminsMapper.insertAll(admins);
    }

    @Override
    public void update(Admins admins) { adminsMapper.update(admins); }

    @Override
    public void delete(Admins admins) { adminsMapper.delete(admins); }

    @Override
    public List<Admins> selectAdmins(Admins admins) {
        List<Admins> result = new ArrayList<>();

        // 如果 loginName 和 permissionLevel 都有值,则根据两个条件进行查询
        if (hasValue(admins.getLoginName()) && admins.getPermissionLevel() != null) {
            result = adminsMapper.selectByLoginNameAndPermissionLevel(admins);
        }
        // 如果只有 loginName 有值,则只根据 loginName 进行查询
        else if (hasValue(admins.getLoginName())) {
            result = adminsMapper.selectByLoginName(admins.getLoginName());
        }
        // 如果只有 permissionLevel 有值,则只根据 permissionLevel 进行查询
        else if (admins.getPermissionLevel() != null) {
            result = adminsMapper.selectByPermissionLevel(admins.getPermissionLevel());
        }

        return result;
    }

    private boolean hasValue(String str) {
        return str != null && !str.isEmpty();
    }
}
